package com.six666.medicaldevicetestingsystem.controller;

import com.six666.medicaldevicetestingsystem.service.Excel_Service;
import com.six666.medicaldevicetestingsystem.service.Export_Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
public class File_Controller {
    String uploadDir;
    @Autowired
    Export_Service export_service;
    @Autowired
    Excel_Service excel_service;
    @Autowired
    public void FileStorageService(Environment env) {
        this.uploadDir = env.getProperty("file.upload-dir");
    }
    @PostMapping("/download")
    public ResponseEntity<byte[]> downloadFile(@RequestParam String fileName) throws IOException {
        Path filePath = Paths.get(this.uploadDir + "\\" + fileName);
        System.out.println(filePath);
        File file = filePath.toFile();

        if (!file.exists()) {
            System.out.println("File does not exist");
            return ResponseEntity.notFound().build();
        }

        byte[] fileContent = Files.readAllBytes(filePath);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getName() + "\"");
        headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);

        return ResponseEntity.ok()
                .headers(headers)
                .body(fileContent);
    }
    @GetMapping("/export_equip_excel")
    public ResponseEntity<byte[]> exportEquipExcel() throws IOException {
        List<Map<String,String>>list=export_service.export_equip_order();
        String[]header=export_service.get_equip_head();
        String type="equip";
        File uploadDir = new File(this.uploadDir+"/temp");
        return getExportExcelResponseEntity(list, header, uploadDir, type);
    }
    @GetMapping("/export_people_excel")
    public ResponseEntity<byte[]> export_people_excel() throws IOException {
        List<Map<String, String>> list = export_service.export_people_order();
        String[] header = export_service.get_people_order();
        File uploadDir = new File(this.uploadDir + "/temp");
        String type="people";
        return getExportExcelResponseEntity(list, header, uploadDir, type);
    }
    @GetMapping("/export_sample_excel")
    public ResponseEntity<byte[]> export_sample_excel() throws IOException {
        List<Map<String, String>> list = export_service.export_sample_order();
        String[] header = export_service.get_sample_head();
        File uploadDir = new File(this.uploadDir + "/temp");
        String type="sample";
        return getExportExcelResponseEntity(list, header, uploadDir, type);
    }
    @GetMapping("/export_message_excel")
    public ResponseEntity<byte[]> export_message_excel() throws IOException {
        List<Map<String, String>> list = export_service.export_message_list();
        String[] header = export_service.get_message_head();
        File uploadDir = new File(this.uploadDir + "/temp");
        String type="message";
        return getExportExcelResponseEntity(list, header, uploadDir, type);
    }
    @GetMapping("/export_compare_plan_excel")
    public ResponseEntity<byte[]> export_compare_plan_excel() throws IOException {
        List<Map<String, String>> list = export_service.export_compare_plan_order();
        String[] header = export_service.get_compare_plan_order();
        File uploadDir = new File(this.uploadDir + "/temp");
        String type="compare_plan";
        return getExportExcelResponseEntity(list, header, uploadDir, type);
    }

    private ResponseEntity<byte[]> getExportExcelResponseEntity(List<Map<String, String>> list, String[] header, File uploadDir, String type) throws IOException {
        if (!uploadDir.exists()) {
            uploadDir.mkdirs();
        }
        try {
            excel_service.exportListToExcel(list, header, this.uploadDir + "/temp/" + type + ".xlsx");
        } catch (IOException e) {
            e.printStackTrace();
        }
        Path filePath = Paths.get(this.uploadDir + "/temp/" + type + ".xlsx");
        System.out.println(filePath);
        File file = filePath.toFile();

        if (!file.exists()) {
            System.out.println("File does not exist");
            return ResponseEntity.notFound().build();
        }

        byte[] fileContent = Files.readAllBytes(filePath);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getName() + "\"");
        headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);

        return ResponseEntity.ok()
                .headers(headers)
                .body(fileContent);
    }
}
